# 保证执行成功
import time

from tools.dingdingrobot import call_dingtalk


def execute_decorator(func):
    def wrapper(*args, **kwargs):
        result = None
        fail_count = 0
        while True:
            try:
                result = func(*args, **kwargs)
                if func.__name__ == 'set_lever':
                    if int(result['code']) == 0:
                        call_dingtalk("杠杆设置成功", 'text')
                        return result
                elif func.__name__ == 'get_current_price':
                    if float(result) > 0:
                        call_dingtalk("当前价格获取结果为 {}".format(result), 'text')
                        return result
                else:
                    return result
            except:
                # logging.debug(str(result))
                time.sleep(5)
                fail_count += 1
                if fail_count == 5:
                    call_dingtalk("Execute failed, result is {}, func name is {}".format(result, func.__name__), 'text')

    return wrapper